// Copyright (c) 2008, 
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.manipulations
{
	import com.faindu.editImage.EditImage;
	
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
/**
 * <code>ImageManipulations</code> handles the manipulations for component external 
 * use. It offers the following:
 * 
 * <p>
 * <ul>
 * <li>Serialize the state of the image manipulations.</li>
 * <li>Reset the manipulations to the image.</li>
 * </ul>
 * </p> 
 */
public class ImageManipulations implements IImageManipulations
{
	/**
	 * Constructor.
	 * 
	 * @param editImage
	 */
	function ImageManipulations( editImage:EditImage ): void
	{
		this.editImage = editImage	
	}
	
	/**
	 * Stores a reference to the host
	 */
	private var editImage: EditImage

	//==========================================================================
	//
	//  Serialize the state of the image manipulations
	//
	//--------------------------------------------------------------------------  		
	/**
	 * @inheritDoc
	 */	
	public function get imageEdits(): XML
	{
		var toolMatrix: Matrix = editImage.toolMatrix
		var cutout: Rectangle = editImage.imageHolder.scrollRect
		var cutouXML: XML
		if ( cutout )
			cutouXML = <cutout 	x={ cutout.x }
								y={ cutout.y }
								width={ cutout.width }
								height={ cutout.height }
								/>

		return 	<imageedit>
					<matrix a={ toolMatrix.a }
							b={ toolMatrix.b }
							c={ toolMatrix.c }
							d={ toolMatrix.d }
							tx={ toolMatrix.tx }
							ty={ toolMatrix.ty }
							/>
					{ cutout ? cutouXML : "" }
				</imageedit>
	}
	public function set imageEdits( edits:XML ): void
	{
		if ( edits == null || edits == "" )
		{
			resetToDefaults()
			return
		}
		
		// update the matrix
		lastImageEdits = edits.copy()
		var toolMatrixXML:XML = edits.matrix[0]
		var newMatrix:Matrix = new Matrix( toolMatrixXML.@a, 
											toolMatrixXML.@b, 
											toolMatrixXML.@c, 
											toolMatrixXML.@d, 
											toolMatrixXML.@tx, 
											toolMatrixXML.@ty )
		
		// store new matrix and apply it to the imageHolder
		editImage.toolMatrix = newMatrix
		editImage.imageHolder.transform.matrix = newMatrix.clone()
		
		// update the cutout 									
		var cutoutXML: XML = edits.cutout[0]
		var cutout:Rectangle 
		if ( cutoutXML )
		{
			cutout = new Rectangle( cutoutXML.@x, 
									cutoutXML.@y, 
									cutoutXML.@width, 
									cutoutXML.@height )
			editImage.imageHolder.scrollRect = cutout
		}
		else
			editImage.imageHolder.scrollRect = null
		
		// calculate the positions of the controls
		editImage.updateControlsPositions()

		// reset tools that rotate the controls
		editImage.rotateTool.reset()
		editImage.cropTool.reset()
		editImage.scaleTool.reset()
		
		// redraw the all controls
		editImage.drawToolControls()
		
		// reset values.
		editImage.rotateTool.deltaAngle = 0
		editImage.invalidateSize()
	}

    //==========================================================================
    //
    //  Reset manipulations
    //
    //--------------------------------------------------------------------------  	
	private var lastImageEdits:XML
	/**
	 * @inheritDoc
	 * 
	 * @see #imageEdits
	 * @see #resetToDefaults()
	 */
	public function reset(): void
	{
		if ( lastImageEdits )
		{
			editImage.imageHolder.scrollRect = null
			editImage.toolMatrix = new Matrix()
			imageEdits = lastImageEdits
		}
	}
	
	/**
	 * @inheritDoc
	 * 
	 * @see #imageEdits 
	 * @see #reset()
	 */
	public function resetToDefaults(): void
	{
		editImage.imageHolder.scrollRect = null
		editImage.toolMatrix = new Matrix()
		imageEdits = imageEdits
	}

    //==========================================================================
    //
    //  Disable all tools
    //
    //--------------------------------------------------------------------------  	
	/**
	 * @inheritDoc
	 */
	public function disableAllTools(): void
	{
		if ( editImage.toolsInitialized )
		{
			editImage.scaleTool.enabled = false
			editImage.cropTool.enabled = false
			editImage.moveTool.enabled = false
			editImage.rotateTool.enabled = false
			editImage.outlineTool.enabled = false
		}
	}

}
}